package com.isasc.demo.pooled.datasource;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: ywy
 * @date: 2022-10-17
 * @desc:
 */
@Endpoint(id = "datasource")
@Component
public class MyEndPoint {

    @Resource
    private DataSource dataSource;

    @ReadOperation
    public Map<String, Object> statics() {
        MyDataSource myDataSource = (MyDataSource) dataSource;
        GenericObjectPool<MyConnection> pool = myDataSource.getPool();
        long createdCount = pool.getCreatedCount();
        int numActive = pool.getNumActive();
        int numIdle = pool.getNumIdle();
        Map<String, Object> map = new HashMap<>();
        map.put("total", createdCount);
        map.put("active", numActive);
        map.put("idle", numIdle);
        return map;
    }
}
